package com.hdjk.view.activity.investment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.dialog.TextButtonDialog;
import com.hdjk.view.network.Network;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.component.CustomView.NoScrollViewPager;
import com.lst.projectlib.entity.BaseEntity;
import com.lst.projectlib.entity.Msg;
import com.lst.projectlib.util.DensityUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.hdjk.view.R.id.tv_sure;

/**
 * 风险评测页
 * Created by sxy on 2017/11/2.
 */

public class ZHDRiskTestActivity extends BaseActivity implements View.OnClickListener {
    private List<View> viewList;
    private List<Boolean> canScrollItem;
    private ArrayList<String[]> contentList;
    private int[] indexs;
    private TextButtonDialog dialog;

    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private NoScrollViewPager vp_list;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        vp_list = (NoScrollViewPager) findViewById(R.id.vp_list);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_zhd_risk_test);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        ViewGroup.LayoutParams layoutParams = vp_list.getLayoutParams();
        layoutParams.width = dm.widthPixels- DensityUtil.dip2px(this,60);
        layoutParams.height = (int) ((float) (layoutParams.width) / 918 * 1277);
        vp_list.setLayoutParams(layoutParams);
    }

    @Override
    protected void initData() {
        viewList = new ArrayList<>();
        canScrollItem = new ArrayList<>();
        contentList = new ArrayList<>();
        indexs = new int[5];
        String[] con1 = new String[5];
        con1[0] = "您的可投资资金大约为：";
        con1[1] = "10万元以下";
        con1[2] = "10万-50万";
        con1[3] = "50万-100万";
        con1[4] = "100万以上";
        contentList.add(con1);
        canScrollItem.add(false);
        String[] con2 = new String[5];
        con2[0] = "您面临一项可能的投资损失，以下哪种情况是您最容易接受的：";
        con2[1] = "肯定会损失600元";
        con2[2] = "20%的机会损失6600元";
        con2[3] = "1%的机会损失88000元";
        contentList.add(con2);
        canScrollItem.add(false);
        String[] con3 = new String[5];
        con3[0] = "您持有的一项投资在过去三年第一年赚11%，第二年赚10%，第三年赚12%，您更倾向于：";
        con3[1] = "加仓买入";
        con3[2] = "减仓卖出";
        contentList.add(con3);
        canScrollItem.add(false);
        String[] con4 = new String[5];
        con4[0] = "您认为自己更偏向是一个：";
        con4[1] = "长期投资者，寻求资产的长期价值升值来获利";
        con4[2] = "短期交易者，利用资产的短期波动频繁交易获利";
        contentList.add(con4);
        canScrollItem.add(false);
        String[] con5 = new String[5];
        con5[0] = "当您的总投资资产浮亏多少时您非常焦虑：";
        con5[1] = "即使是非常小的亏损也会很焦虑";
        con5[2] = "亏损超过10%";
        con5[3] = "亏损超过20%";
        con5[4] = "亏损超过30%";
        contentList.add(con5);
        canScrollItem.add(false);
        initPager(contentList);
        vp_list.setOffscreenPageLimit(5);
        vp_list.setAdapter(new MyViewPagerAdapter());
        vp_list.setCanScrollItem(canScrollItem);
    }

    private void initPager(List<String[]> list) {
        for (int i = 0; i < list.size(); i++) {
            String[] contents = list.get(i);
            View view = LayoutInflater.from(this).inflate(R.layout.layout_risk_test, null);
            ((TextView) view.findViewById(R.id.tv_title)).setText(contents[0]);
            if (TextUtils.isEmpty(contents[1])) {
                view.findViewById(R.id.tv_content1).setVisibility(View.GONE);
            } else {
                ((TextView) view.findViewById(R.id.tv_content1)).setText(contents[1]);
                view.findViewById(R.id.tv_content1).setOnClickListener(this);
            }
            if (TextUtils.isEmpty(contents[2])) {
                view.findViewById(R.id.tv_content2).setVisibility(View.GONE);
            } else {
                ((TextView) view.findViewById(R.id.tv_content2)).setText(contents[2]);
                view.findViewById(R.id.tv_content2).setOnClickListener(this);
            }
            if (TextUtils.isEmpty(contents[3])) {
                view.findViewById(R.id.tv_content3).setVisibility(View.GONE);
            } else {
                ((TextView) view.findViewById(R.id.tv_content3)).setText(contents[3]);
                view.findViewById(R.id.tv_content3).setOnClickListener(this);
            }
            if (TextUtils.isEmpty(contents[4])) {
                view.findViewById(R.id.tv_content4).setVisibility(View.GONE);
            } else {
                ((TextView) view.findViewById(R.id.tv_content4)).setText(contents[4]);
                view.findViewById(R.id.tv_content4).setOnClickListener(this);
            }
            switch (i) {
                case 0:
                    view.setBackgroundDrawable(getResources().getDrawable(R.drawable.pager_bg1));
                    ((ImageView) view.findViewById(R.id.iv_1)).setImageDrawable(getResources().getDrawable(R.drawable.pager_button_xuanzhong));
                    break;
                case 1:
                    view.setBackgroundDrawable(getResources().getDrawable(R.drawable.pager_bg2));
                    ((ImageView) view.findViewById(R.id.iv_2)).setImageDrawable(getResources().getDrawable(R.drawable.pager_button_xuanzhong));
                    break;
                case 2:
                    view.setBackgroundDrawable(getResources().getDrawable(R.drawable.pager_bg3));
                    ((ImageView) view.findViewById(R.id.iv_3)).setImageDrawable(getResources().getDrawable(R.drawable.pager_button_xuanzhong));
                    break;
                case 3:
                    view.setBackgroundDrawable(getResources().getDrawable(R.drawable.pager_bg4));
                    ((ImageView) view.findViewById(R.id.iv_4)).setImageDrawable(getResources().getDrawable(R.drawable.pager_button_xuanzhong));
                    break;
                case 4:
                    view.setBackgroundDrawable(getResources().getDrawable(R.drawable.pager_bg5));
                    ((ImageView) view.findViewById(R.id.iv_5)).setImageDrawable(getResources().getDrawable(R.drawable.pager_button_xuanzhong));
                    break;
            }
            viewList.add(view);
        }
    }

    @Override
    public void onClick(View v) {
        int index = 5;
        switch (v.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.tv_content1:
                index--;
            case R.id.tv_content2:
                index--;
            case R.id.tv_content3:
                index--;
            case R.id.tv_content4:
                index--;
                canScrollItem.set(vp_list.getCurrentItem(), true);
                View currentView = viewList.get(vp_list.getCurrentItem());
                if (currentView.findViewById(R.id.tv_content1).getVisibility() == View.VISIBLE) {
                    ((TextView) currentView.findViewById(R.id.tv_content1)).setTextColor(getResources().getColor(R.color.gray_aaaaaaa));
                }
                if (currentView.findViewById(R.id.tv_content2).getVisibility() == View.VISIBLE) {
                    ((TextView) currentView.findViewById(R.id.tv_content2)).setTextColor(getResources().getColor(R.color.gray_aaaaaaa));
                }
                if (currentView.findViewById(R.id.tv_content3).getVisibility() == View.VISIBLE) {
                    ((TextView) currentView.findViewById(R.id.tv_content3)).setTextColor(getResources().getColor(R.color.gray_aaaaaaa));
                }
                if (currentView.findViewById(R.id.tv_content4).getVisibility() == View.VISIBLE) {
                    ((TextView) currentView.findViewById(R.id.tv_content4)).setTextColor(getResources().getColor(R.color.gray_aaaaaaa));
                }
                ((TextView) v).setTextColor(getResources().getColor(R.color.bg_common_red));
                indexs[vp_list.getCurrentItem()] = index;
                if (vp_list.getCurrentItem() + 1 < vp_list.getChildCount()) {
                    vp_list.setCurrentItem(vp_list.getCurrentItem() + 1);
                } else if (vp_list.getCurrentItem() + 1 == vp_list.getChildCount()) {
                    dialog = new TextButtonDialog(ZHDRiskTestActivity.this, R.style.progress_dialog, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            switch (v.getId()) {
                                case R.id.tv_cancel:
                                    dialog.dismiss();
                                    vp_list.setCurrentItem(0);
                                    break;
                                case tv_sure:
                                    dialog.dismiss();
                                    submit();
                                    break;
                            }
                        }
                    });
                    dialog.setDialogContent("测评结束，是否需要重新测评？", 15);
                    dialog.setButtonText("重新测评", "提交");
                    dialog.show();
                }
                break;
        }
    }

    private void submit() {
        mDialog.show();
        JSONObject object = new JSONObject();
        try {
            object.put("1", getIndex(indexs[0]));
            object.put("2", getIndex(indexs[1]));
            object.put("3", getIndex(indexs[2]));
            object.put("4", getIndex(indexs[3]));
            object.put("5", getIndex(indexs[4]));
            Network.submitZHDRiskTest(UserTokenUtil.getToken(this), object.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    mDialog.cancel();
                    if (msg!=null&&msg.getSuccess()&&msg.getData()!=null&&msg.getData() instanceof BaseEntity){
                        Intent intent=new Intent(ZHDRiskTestActivity.this,ZHDRiskTestReturnActivity.class);
                        intent.putExtra("type",((BaseEntity) msg.getData()).getName());
                        startActivity(intent);
                        back();
                    }else {
                        if (msg!=null&&!TextUtils.isEmpty(msg.getMsg())&&!"null".equals(msg.getMsg())){
                            ToastUtils.showShortToast(ZHDRiskTestActivity.this,msg.getMsg());
                        }else {
                            ToastUtils.showShortToast(ZHDRiskTestActivity.this,"提交失败");
                        }
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {
                    mDialog.cancel();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private String getIndex(int index) {
        String ind = "";
        switch (index) {
            case 1:
                ind = "a";
                break;
            case 2:
                ind = "b";
                break;
            case 3:
                ind = "c";
                break;
            case 4:
                ind = "d";
                break;
        }
        return ind;
    }

    class MyViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            view.addView(viewList.get(position));
            return viewList.get(position);
        }
    }
}
